package com.blovesoft.tokenup.util;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.StringUtils;
import com.blovesoft.tokenup.R;

/**
 * author: zhu.px
 * e-mail: 1271901673@qq.com
 * time  : 2018/5/28
 * desc  : dialog封装
 */
public class DialogHelper {

    public static void showDialog(Context context, String content, String positiveText, String negativeText, DialogInterface dialogInterface) {
        showDialog(context, "", content, positiveText, negativeText, dialogInterface);
    }

    public static void showDialog(Context context, String title, String content, String positiveText, String negativeText, final DialogInterface dialogInterface) {
        MaterialDialog.Builder builder = new MaterialDialog.Builder(context);
        builder.content(content);
        if (!StringUtils.isEmpty(title)) {
            builder.title(title);
        }
        if (!StringUtils.isEmpty(positiveText)) {
            builder.positiveText(positiveText);
        } else {
            builder.positiveText(context.getResources().getString(R.string.confirm));
        }
        if (!StringUtils.isEmpty(negativeText)) {
            builder.negativeText(negativeText);
        } else {
            builder.negativeText(context.getResources().getString(R.string.cancel));
        }
        builder.onPositive((dialog, which) -> {
            if (dialogInterface != null) {
                dialogInterface.onPositive();
            }
        }).onNegative((dialog, which) -> {
            if (dialogInterface != null) {
                dialogInterface.onNegative();
            }
        }).positiveColorRes(R.color.colorPrimary).negativeColorRes(R.color.colorPrimary).show();
    }

    public static void showSingleDialog(Context context, String content) {
        new MaterialDialog.Builder(context).content(content).title(context.getResources().getString(R.string.tip))
                .positiveText(context.getResources().getString(R.string.confirm))
                .positiveColorRes(R.color.colorPrimary).show();
    }

    /**
     * 打开app设置界面
     *
     * @param mContext
     */
    public static void showPermissionDialog(final Context mContext, String tip) {
        showDialog(mContext, tip, new DialogInterface() {
            @Override
            public void onPositive() {
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.setData(Uri.parse("package:" + mContext.getPackageName()));
                mContext.startActivity(intent);
            }

            @Override
            public void onNegative() {

            }
        });
    }

    public static void showDialog(Context context, String content, DialogInterface dialogInterface) {
        showDialog(context, "", content, dialogInterface);
    }

    public static void showDialog(Context context, String title, String content, DialogInterface dialogInterface) {
        showDialog(context, title, content, "", "", dialogInterface);
    }

    public interface DialogInterface {
        void onPositive();

        void onNegative();
    }

}
